package com.zym.service.impl;

import com.zym.config.ProProperties;
import com.zym.enums.AckCode;
import com.zym.service.FileUpload2Service;
import com.zym.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FileUpload2ServiceImpl implements FileUpload2Service {
    @Autowired
    private ProProperties proProperties;
    @Override
    public Boolean fileUpload2(MultipartFile file) {
        int size=5*1024*1024;
        String OutPath=proProperties.getOutPath();
        // 检查输出目录是否存在，不存在则创建
        File outDir = new File(OutPath);
        if (!outDir.exists()) {
            outDir.mkdirs();
        }
        try(BufferedInputStream bis=new BufferedInputStream(file.getInputStream())) {

            int split=1;
            int readByte=0;
            byte[] buffer = new byte[size];
            while ((readByte=bis.read(buffer))!=-1){
                String FileOutPath=OutPath+"part_"+split+"_"+file.getOriginalFilename();
                try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(FileOutPath));){
                    bos.write(buffer,0,readByte);
                    System.out.printf("已拆分文件: %s%d (大小: %d bytes)%n",
                            FileOutPath, split, readByte);
                    bos.flush();  // 确保数据写入磁盘
                    split++;
                }
            }
        }catch (Exception e){
            System.err.println("文件操作出错: " + e.getMessage());
            e.printStackTrace();
            return  false;
        }
        return true;
    }

    @Override
    public Boolean fileMerge() {
        String FilePath=proProperties.getOutPath();
        File file = new File(FilePath);
        if (!file.exists()){
            System.out.println("文件路径不存在");
            return false;
        }
        String[] part_s = file.list((dir, name) -> {
            return name.contains("part_");
        });
        System.out.println(part_s);
        String hz=part_s[0].substring(part_s[0].indexOf("."));
        List<String> collect = Arrays.asList(part_s).stream().sorted().collect(Collectors.toList());
        System.out.println(collect);
        try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(FilePath+"_merge"+hz))){
            for (int i = 0; i < collect.size(); i++) {
                File file1=new File(FilePath+"\\"+collect.get(i));

                try (BufferedInputStream bis = new BufferedInputStream(  new FileInputStream( file1))) {
                    byte[] readByte=new byte[4096];
                    int cc=0;
                    while ((cc=bis.read(readByte))!=-1){
                        bos.write(readByte,0,cc);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    return false;
                }
                //删除分片文件
                file1.delete();
            }
            bos.flush();



        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
